﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PRODUCT.Product.database;

namespace ERP_RORZE_PRODUCT.Product.Machine
{
    public partial class frm_MachineGroupEdit : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler Reload;
        ClsMachineDAO clsmachineDAO = new ClsMachineDAO();
        MachineGroup b = new MachineGroup();
        DataTable dt;
        private LogUtils log;

        public string ID;
        public frm_MachineGroupEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtMachineGP.Text == "")
            {
                MessageBox.Show("Chưa nhập MachineGP");
            }
            else if (txtMaQuantiy.Text == "")
            {
                MessageBox.Show("Chưa nhập MaQuantiy");
            }
            else
            {
                try
                {
                    b.Content = txtContent.Text;
                    b.MachineGP = txtMachineGP.Text;
                    b.MaQuantiy = int.Parse(txtMaQuantiy.Text);
                    b.MGroupName = txtMGroupName.Text;
                    clsmachineDAO.UpdateMachineGroup(b);

                    //write Log
                    log.Table = LogConstant.Table.Product.MachineGroup;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "MachineGP: " + b.MachineGP + "; MaQuantiy: " + b.MaQuantiy + "; MGroupName: " + b.MGroupName;
                    log.WriteLog();

                    MessageBox.Show("Đã nhập dữ liệu xong");
                    Reload(sender, e);
                }
                catch
                {
                    MessageBox.Show("MaQuantiy phải là kiểu số");
                }
            }
        }

        private void frm_MachineGroupEdit_Load(object sender, EventArgs e)
        {
            b.MachineGP = ID;
            dt = clsmachineDAO.GetMachineGRByID(b);
            foreach (DataRow r in dt.Rows)
            { 
                txtContent.Text = r["Content"].ToString();
                txtMachineGP.Text = r["MachineGP"].ToString();
                txtMaQuantiy.Text = r["MaQuantiy"].ToString();
                txtMGroupName.Text = r["MGroupName"].ToString();
            }
        }
    }
}